package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void SetmealAdd(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        Long SetmealId = setmealMapper.selectByName(setmealDTO.getName());
        if (setmealDishes==null||setmealDishes.isEmpty()){
            return;
        }
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(SetmealId);
            setmealDishMapper.insert(setmealDish);
        }

    }

    @Override
    public PageResult SetmealPageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<SetmealVO> setmealVOList = setmealDishMapper.select(setmealPageQueryDTO);
        for (SetmealVO setmealVO : setmealVOList) {
            Long categoryId = setmealVO.getCategoryId();
            String name= categoryMapper.select(categoryId);
            setmealVO.setCategoryName(name);
        }
        Page<SetmealVO> page = new Page(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        page.addAll(setmealVOList);
        page.setTotal(setmealVOList.size());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public SetmealVO SetmealByid(Long id) {
        SetmealVO setmealVO = setmealMapper.select(id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishList);
        Long categoryId = setmealVO.getCategoryId();
        String name = categoryMapper.select(categoryId);
        setmealVO.setCategoryName(name);
        return setmealVO;
    }

    @Override
    public void SetmealDelete(List<Long> ids) {
        //起售中的套餐不能删除
        for (Long id : ids) {
            SetmealVO setmealVO = setmealMapper.select(id);
            if (setmealVO.getStatus()==1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            setmealMapper.deleteSetmeal(id);
            setmealDishMapper.deleteBySetmealId(id);
        }
        //要把相关菜品表一起删除
    }

    @Override
    public void IsSale(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.Update(setmeal);
    }

    @Override
    public void SetmealUpdate(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.Update(setmeal);
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
